# -*- coding: utf-8 -*-

from onlinebrary.lib.base import *

import logging
log = logging.getLogger(__name__)

class ConverterController(BaseController):

    def __init__(self):
        pass
    
    def xml_to_fb2(self):
        try:
            if request.params.has_key('url'):
                url = request.params['url']
                filename = g.feed_to_fb2.convert(url, True)
                if not filename:
                    return g.viewer.parse_error("Ошибка или пустой поток. Извините.")
                c.absolute_filename = filename
                return render('/converter/xml_to_fb2.mako')
            return g.viewer.parse_error("Нечего конвертировать :(")
        except Exception, e:
            log.error(e)
            return g.default_error_msg
    
    def html_to_pdf(self):
        try:
            if request.params.has_key('url'):
                url = request.params['url']
                filename = g.html_to_pdf.convert(url)
                if not filename:
                    return "Ошибка или пустой поток. Извините."
                c.absolute_filename = filename
                return render('/converter/html_to_pdf.mako')
            return g.viewer.parse_error("Нечего конвертировать :(")
        except Exception, e:
            log.error(e)
            return g.viewer.parse_error(g.default_error_msg)
    
